package com.banciyuan.bcywebview.biz.exhibition;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.PartyApi;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.ProgressbarHelper;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.base.util.BaseWatcher;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.OnErrorProxy;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.List;

import de.greenrobot.daoexample.model.Location;

/**
 * Created by squall on 2016/8/31.
 */
public class ExhibitionLocationActivity extends BaseActivity implements BaseWatcher {

    private ActionbarHelper mActionbarHelper;
    private View mActionBarView;
    private ProgressbarHelper mProgressbarHelper;
    private View mProgressbarView;
    private RequestQueue queue;
    private String city;

    private ListView mlistView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_teaminfo);
        initArgs();
        initProgressbar();
        initActionbar();
        initUi();
        initData();
    }

    @Override
    protected void initUi() {
        mlistView = (ListView) findViewById(R.id.listView);
    }

    @Override
    protected void initArgs() {
        queue = VolleyQueue.getRquest(this);
        city = getIntent().getStringExtra(gotoUtil.INTENT_VALUE_ONE);
        if (TextUtils.isEmpty(city)) {
            city = getString(R.string.goble);
        }
    }

    @Override
    protected void initActionbar() {
        mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.exhibition_area));
    }

    @Override
    protected void initProgressbar() {
        mProgressbarView = findViewById(R.id.base_progressbar);
        mProgressbarHelper = new ProgressbarHelper(mProgressbarView);
        mProgressbarHelper.setActionCallbacks(new ProgressbarHelper.OnProgressCallbacks() {
            @Override
            public void onActionClick(int id) {
                mProgressbarHelper.onLoading();
                initData();
            }
        });
        mProgressbarHelper.onLoading();
    }

    @Override
    protected void onResume() {
        super.onResume();
        BaseObserver.getInstance().addWatcher(ExhibitionLocationActivity.class, this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        BaseObserver.getInstance().removeWatcher(ExhibitionLocationActivity.class);
    }

    @Override
    protected void initData() {
        getArea();
    }

    private void getArea() {
        String url = HttpUtils.BASE_URL + PartyApi.getPartyArea();
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    if (StatusCodeUtils.dealStatusCode(response, ExhibitionLocationActivity.this)) {
                        JSONObject jsonObject = new JSONObject(response);
                        Gson gson = new Gson();
                        List<Location> llist = gson.fromJson(jsonObject.getString(HttpUtils.UPLOAD_DATA), new TypeToken<List<Location>>() {
                        }.getType());
                        renderData(llist);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mProgressbarHelper.onFailed();
            }
        };
        OnErrorProxy onErrorProxy = new OnErrorProxy(onError, onSucc, url, this, null);
        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, null, onSucc, onErrorProxy);
        queue.add(stringRequestParam);
    }

    private void renderData(List<Location> llist) {
        mlistView.setAdapter(new ExhibitionLocationAdapter(llist, this, city));
        mProgressbarHelper.onSuccess();
    }


    @Override
    public void onEventHappen(int eventCode, String message, Object object) {
        switch (eventCode) {
            case BaseObserver.CITY_CILICK:
                Location.Local local = (Location.Local) object;
                Intent intent = new Intent();
                intent.putExtra("city", local.getCity());
                intent.putExtra("city_id", local.getCity_id());
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }
}
